 --[[
任务模块组相关逻辑
--]] --日常任务逻辑
local Lang = require("common.Lang")
 local ExcelTaskConf = require("excel.Task")
local ObjHuman = require("core.ObjHuman")
local TaskHelp = require("task.TaskHelp")
local TaskDefine = require("task.Define")
local TaskDailyStatus = require("task.TaskDailyStatus")
local TaskLogic = require("task.TaskLogic")
local ShopHelp = require("shop.ShopHelp")
local ActivityLogicL = require("activity.ActivityLogicL")
local VipDefine = require("vip.VipDefine")
local VipLogic  = require("vip.VipLogic")
local MonthCardLogic = require("vip.MonthCardLogic")
local Broadcast = require("broadcast.Broadcast")
Json = Json or require("common.Json")
local Msg = require("core.Msg")

task_pool_data = task_pool_data or {} -- 任务池数据 groupID->taskID 每天随机生成


function taskMaxGroupCnt(human)
  return TaskDefine.DAILY_GROUP_DO_MAX_CNT + VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGE_DAILYTASK)
end

function addCnt(human)
  if human.db.taskGroupAddCnt >= VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGE_DAILYTASK) then
      Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TASK_ADD_CNT_NOT_VIP)
      return
  end
  if ObjHuman.getYuanbao(human) < TaskDefine.DAILY_GROUP_ADD_CNT_RMB then
      ShopHelp.onNoRMB(human)
      return
  end
  ObjHuman.decYuanbao(human, TaskDefine.DAILY_GROUP_ADD_CNT_RMB, "task_daily_add")
  human.db.taskGroupAddCnt = human.db.taskGroupAddCnt + 1
  taskAutoAccept(human)
  TaskDailyStatus.sendTaskDailyStatus(human)
  TaskHelp.refreshDoing(human)
end

function taskAutoAccept(human)
   local task = TaskLogic.getTaskGroupTask(human)
   if task and task.status == TaskDefine.TASK_ACCEPTED then
      return
   end
   local last_task_id 
   if task then
      last_task_id = task.taskid
      TaskLogic.delTaskGroupTask(human)
   end
   if VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGE_DAILYTASK) >0 and human.db.taskGroupCnt >= taskMaxGroupCnt(human) then
      return --没有了
   end
   
   local group_id  --下一环组ID
   local group
   local huan = human.db.taskGroupCnt+1
   if last_task_id and ExcelTaskConf.Task[last_task_id] then
      group_id = ExcelTaskConf.Task[last_task_id].groupId
      group = ExcelTaskConf.TaskGroup[group_id]
      if group and ExcelTaskConf.TaskGroup[group.nextID] then
          group_id = group.nextID
          group = ExcelTaskConf.TaskGroup[group_id]
          if group.lvUpLimit < human.db.lv then
            group_id = nil
          end
      else
          group_id = nil
          group = nil
      end
   end
   if group_id == nil then
      for k,group in pairs(ExcelTaskConf.TaskGroup) do
          if group.lvUpLimit >= human.db.lv and group.num == huan then
              local taskid = task_pool_data[k]
              if taskid and ExcelTaskConf.Task[taskid] and TaskLogic.taskCanAccept(human,taskid) == TaskDefine.TASK_ACCEPT_OK then
                  group_id = k
                  break
              end
          end
      end
   end   
   if group_id == nil then  --没有可接日常任务
      return
   end
   group = ExcelTaskConf.TaskGroup[group_id]
   local taskid = task_pool_data[group_id]    
   if taskid == nil then
      return
   end
   
   if human.db.taskGroupCnt == TaskDefine.DAILY_GROUP_DO_MAX_CNT and human.db.taskGroupCnt >= taskMaxGroupCnt(human) then
      TaskHelp.refreshCanDoTask(human, taskid)  --VIP才可接
      return 1, taskid
   else
      TaskLogic.taskDoAccept(human,taskid)
      return 2, taskid
   end   
end


function taskFinish(human, taskID, taskConfig)
	local groupID = taskConfig.groupId
	if groupID == 0 then return end	
	
	human.db.taskGroupCnt = human.db.taskGroupCnt + 1

  taskAutoAccept(human)

	TaskDailyStatus.sendTaskDailyStatus(human)
	
	TaskHelp.refreshDoing(human)
	
	ActivityLogicL.updateActivityValue(human, "daily_task")
	
end

function onChangeVip(human)
  TaskDailyStatus.sendTaskDailyStatus(human)  
  local flag,taskID = taskAutoAccept(human)
  if flag == 2 then
      local msgRet = Msg.gc.GC_TASK_ACCEPT
      msgRet.taskID = taskID
      msgRet.retCode = TaskDefine.TASK_ACCEPT_OK
      Msg.send(msgRet, human.fd)      
      TaskHelp.refreshDoing(human)
  end
  
end
function taskAutoFinish(human, taskID)
  local task = TaskLogic.getTaskGroupTask(human)
  if task == nil or task.status ~= TaskDefine.TASK_ACCEPTED or task.taskid ~= taskID then
      return 1,Lang.TASK_FINISH_FAIL_GROUP_ERR
  end
  if not MonthCardLogic.isHaveSpec(human)  and ObjHuman.getYuanbao(human) < TaskDefine.DAILY_GROUP_AUTO_COMPLETE_RMB then
      ShopHelp.onNoRMB(human)
      return 3,""
  end
  local oldFlag = task.finishFlag
  task.finishFlag = TaskDefine.TASK_FINISH_FLAG_NO_CHECK
  if TaskLogic.taskCanFinish(human, taskID) ~= TaskDefine.TASK_FINISH_OK then
     task.finishFlag  = oldFlag
     return 2, Lang.TASK_FINISH_FAIL_BAG
  end
  if not MonthCardLogic.isHaveSpec(human) then
      ObjHuman.decYuanbao(human, TaskDefine.DAILY_GROUP_AUTO_COMPLETE_RMB, "task_daily")
  end
  TaskLogic.taskDoFinish(human, taskID)
  return 0,string.format(Lang.TASK_FINISH_GROUP_AUTO_OK,ExcelTaskConf.Task[taskID].name)
end

function updateDaily(human)
  human.db.taskGroupCnt  = 0
  human.db.taskGroupAddCnt = 0
  TaskLogic.delTaskGroupTask(human)
end

function synsTaskPool(data)
  task_pool_data = Json.Decode(data)
end

